$(function () {
    obj.init();
});

var obj = new JsFun();
function JsFun() {
    return {
        dataGrid: null,
        school_area:null,
        params: {
            mine: null
        },
        type: null,
        iSchoolId: null,
        init: function () {
            this.type     = xhTool.getQuery('type');//1审核统计页2审核历史页
            this.iSchoolId     = xhTool.getQuery('iSchoolId');
            this.params.mine = xhTool.getQuery('mine') || 0;
            if (this.type == 1){
                this.dataGrid = new xhDataGrid($('#list'), 'Device/getDeviceInfo');
                this.dataGrid.parseToolbar().init().activeButton(['bt_list_check', 'bt_list_remark'], ['bt_list_history', 'bt_list_delCheck']);
            }else {
                this.dataGrid = new xhDataGrid($('#list'), 'Device/getCheckList', {iSchoolId:this.iSchoolId});
                this.dataGrid.parseToolbar().init();
            }
            this.school_area = $('#school_area');
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.listener();
        },
        listener: function () {
            var $this = this;
            if ($this.type === '1') {
                //获取区域数据
                $('#school_area').combotree({
                    url: 'getDepData',
                    animate:true,
                    onLoadSuccess:function(node,data){
                        $length = data.length;
                        $.each(data,function (id,value) {
                            if (value.children !== undefined) {
                                $length = $length + value.children.length;
                            }
                        })
                    },
                    onShowPanel:function(){
                        if ($length <= 8){
                            $(this).combobox('panel').height("auto");//高度自适应
                        }else{
                            $(this).combobox('panel').height("200");//高度自定义
                        }
                    },
                    onBeforeSelect: function (node) {
                        var t = $(this).tree;
                        var isLeaf = t('isLeaf', node.target);
                        if (!isLeaf) {//选择的不是叶子节点
                            return false;
                        }
                    }
                });
            }
            //查询
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().searchList();
                })
            );
            if (this.type == 1){
                $this.dataGrid.onDblClickRow = function(rowIndex, rowData){
                    var iSchoolId = rowData.iSchoolId;
                    var sSchoolName = rowData.sSchoolName;
                    if(!iSchoolId) return false;
                    window.top.xhTop.showModal({
                        title: '领用情况：'+ sSchoolName,
                        content: xhTool.getFrameString('Device/deviceCheckList', {iSchoolId: iSchoolId}, 550),
                        size: 'lg',
                        button: true,
                        backClose: false,
                        // width:900
                    });
                };
            }

            //列表加载后合并单元格
            if (this.type != 1){
                $this.dataGrid.onLoadSuccess = function(data){
                    $this.dataGrid.clear();
                    if (data.rows.length <= 0) return false;
                    $this.dataGrid.mergeCells(['sSchoolName']);
                };
            }
            //审核
            $('#bt_list_check').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().check();
            });
            //备注
            $('#bt_list_remark').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().remarkCheck();
            });
            //历史
            $('#bt_list_history').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().history();
            });
            //删除
            $('#bt_list_delCheck').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().delCheck();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                //查找
                searchList: function () {
                    if ($this.type === '1') {
                        params = {school: xhExtra.getValue($('#list_school')), school_area: $('#school_area').combotree('getValue')};
                    } else {
                        params = {school: xhExtra.getValue($('#list_school'))};
                    }
                    $this.dataGrid.reload(params)
                },
                //审核
                check: function () {
                    var schoolIds = $this.dataGrid.getSelectValues('iSchoolId');
                    if (schoolIds.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        html: '填写审核意见<textarea id="checkRemark" class="swal2-textarea">',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var checkRemark     = $('#checkRemark', window.top.document).val();
                                xhExtra.ajaxRequest('Device/checkInfo', {
                                    schoolIds: schoolIds,
                                    checkRemark: checkRemark
                                }, {success: true});
                                resolve();
                                $this.dataGrid.reload();
                            })
                        }
                    })
                    // xhExtra.askChangeStatus('Device/checkInfo', {schoolIds: schoolIds}, $this.dataGrid, '确认审核？');
                },
                history: function () {
                    var schoolId = $this.dataGrid.getSelectValue('iSchoolId');
                    if(!schoolId){
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '审核历史',
                        content: xhTool.getFrameString('Device/checkList', {iSchoolId: schoolId}, 450),
                        size: 'md',
                        button: true,
                        width:850
                    });
                },
                delCheck: function () {
                    var schoolId = $this.dataGrid.getSelectValue('iSchoolId');
                    var status = $this.dataGrid.getSelectValue('status');
                    if(!schoolId){
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    if (status.indexOf('正常') == -1){
                        window.top.xhTop.showError('没有被审核，请重新选择！');
                        return false;
                    }
                    xhExtra.askChangeStatus('Device/delCheckHistory', {schoolId: schoolId}, $this.dataGrid, '确认删除？');
                },
                remarkCheck: function () {
                    var schoolIds = $this.dataGrid.getSelectValues('iSchoolId');
                    if (schoolIds.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        html: '填写备注<input id="remark" class="swal2-input">',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var remark     = $('#remark', window.top.document).val();
                                // if(remark < 1){
                                //     reject('备注不能为空');
                                // } else {
                                    xhExtra.ajaxRequest('Device/setCheckRemark', {
                                        schoolIds: schoolIds,
                                        remark: remark
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid.reload();
                                // }
                            })
                        }
                    })
                }
            }
        }
    }
}